<template>
  <div class="modal">
    <el-dialog
      v-model="dialogVisible"
      :title="isNewRef ? '新建部门' : '编辑部门'"
      width="30%"
      center
    >
      <div class="form">
        <el-form v-model="formDate" label-width="100px">
          <!-- 部门名称 -->
          <el-form-item label="部门名称" prop="name" size="large">
            <el-input
              v-model="formDate.name"
              placeholder="请输入部门名称"
            ></el-input>
          </el-form-item>
          <!-- 部门领导 -->
          <el-form-item label="选择部门领导" prop="leader" size="large">
            <el-input
              v-model="formDate.leader"
              placeholder="请输入部门领导"
            ></el-input>
          </el-form-item>
          <!-- 选择部门 -->
          <el-form-item label="选择部门" prop="parentId" size="large">
            <el-select
              v-model="formDate.parentId"
              placeholder="请选择部门"
              style="width: 100%"
            >
              <template v-for="item in entireDepartments" :key="item.id">
                <el-option :label="item.name" :value="item.id" />
              </template>
            </el-select>
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="cancelClick">取消</el-button>
          <el-button type="primary" @click="handleConfirm"> 确定 </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script setup lang="ts">
import { storeToRefs } from "pinia"
import { formatUTC } from "@/utils/format"
import useMainStore from "@/stores/main/main"
import useSystemStore from "@/stores/main/system/system"
import { reactive, ref } from "vue"
const dialogVisible = ref(false)
const mainStore = useMainStore()
const systemStore = useSystemStore()
// 判断编辑不需要密码
const isNewRef = ref<boolean>()
// 记录存储编辑用户数据
const editData = ref()
// mainStore.fetchEntireDataAction()
// 获取角色列表
const { entireDepartments } = storeToRefs(mainStore)
const formDate = reactive<any>({
  name: "",
  leader: "",
  parentId: ""
})
function setModalVisible(isNew: boolean, itemData?: any) {
  dialogVisible.value = true
  isNewRef.value = isNew
  if (!isNew && itemData) {
    // console.log("哈哈哈")
    for (const key in itemData) {
      // if(key==="createAt"||key==="updateAt"){
      //   console.log(formatUTC(itemData[key]),"哈哈哈哈")
      //   formDate[key] = formatUTC(itemData[key])
      // }
      formDate[key] = itemData[key]
    }
    editData.value = itemData
  } else {
    for (const key in formDate) {
      formDate[key] = ""
    }
    console.log(formDate)
    editData.value = null
  }
}
// 确定添加数据
function handleConfirm() {
  dialogVisible.value = false
  // console.log(editData.value)
  // console.log(isNewRef)
  // 编辑部门
  if (!isNewRef.value && editData.value) {
    console.log("编辑")
    console.log(formDate)
    systemStore.editPageDataDataAction(
      "department",
      editData.value.id,
      formDate
    )
  } else {
    // 新建部门
    console.log("添加")
    console.log(formDate)
    systemStore.newPageDataAction("department", formDate)
  }
}
// 取消按钮事件
function cancelClick() {
  dialogVisible.value = false
}

defineExpose({
  setModalVisible,
  handleConfirm
})
</script>
<style scoped lang="less">
.modal {
}
.form {
  padding: 0 20px;
}
</style>
